﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002CD RID: 717
	internal class PairwiseObservable<T, TR> : OperatorObservableBase<TR>
	{
		// Token: 0x06000F70 RID: 3952 RVA: 0x00047A8C File Offset: 0x00045E8C
		public PairwiseObservable(IObservable<T> source, Func<T, T, TR> selector) : base(source.IsRequiredSubscribeOnCurrentThread<T>())
		{
			this.source = source;
			this.selector = selector;
		}

		// Token: 0x06000F71 RID: 3953 RVA: 0x00047AA8 File Offset: 0x00045EA8
		protected override IDisposable SubscribeCore(IObserver<TR> observer, IDisposable cancel)
		{
			return this.source.Subscribe(new PairwiseObservable<T, TR>.Pairwise(this, observer, cancel));
		}

		// Token: 0x040008FA RID: 2298
		private readonly IObservable<T> source;

		// Token: 0x040008FB RID: 2299
		private readonly Func<T, T, TR> selector;

		// Token: 0x020002CE RID: 718
		private class Pairwise : OperatorObserverBase<T, TR>
		{
			// Token: 0x06000F72 RID: 3954 RVA: 0x00047AC0 File Offset: 0x00045EC0
			public Pairwise(PairwiseObservable<T, TR> parent, IObserver<TR> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x06000F73 RID: 3955 RVA: 0x00047AF4 File Offset: 0x00045EF4
			public override void OnNext(T value)
			{
				if (this.isFirst)
				{
					this.isFirst = false;
					this.prev = value;
					return;
				}
				TR value2;
				try
				{
					value2 = this.parent.selector(this.prev, value);
					this.prev = value;
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				this.observer.OnNext(value2);
			}

			// Token: 0x06000F74 RID: 3956 RVA: 0x00047B8C File Offset: 0x00045F8C
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06000F75 RID: 3957 RVA: 0x00047BC4 File Offset: 0x00045FC4
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x040008FC RID: 2300
			private readonly PairwiseObservable<T, TR> parent;

			// Token: 0x040008FD RID: 2301
			private T prev = default(T);

			// Token: 0x040008FE RID: 2302
			private bool isFirst = true;
		}
	}
}
